﻿using BO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.system.role
{
    public partial class edit : System.Web.UI.Page
    {
        Maticsoft.BLL.Role role = new Maticsoft.BLL.Role();

        Maticsoft.BLL.Rights rights = new Maticsoft.BLL.Rights();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidid.Value = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(hidid.Value))
                {
                    Maticsoft.Model.Role md = role.GetModel(Convert.ToInt32(hidid.Value));
                    note.Value = md.description;
                    username.Value = md.rolename;
                    code.Value = md.rolecode;
                    //code.Disabled = true;
                }
                rptone.DataSource = rights.GetList("ltype=1");
                rptone.DataBind();
            }
        }

        protected void btnok_Click(object sender, EventArgs e)
        {

        }

        protected void rptone_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rpt = (Repeater)e.Item.FindControl("rpttrue");
                HiddenField hidparentcode = (HiddenField)e.Item.FindControl("hidparentcode");
                rpt.DataSource = rights.GetList("parentcode='" + hidparentcode.Value + "'");
                rpt.DataBind();

            }
        }
        /// <summary>
        /// 获取角色所有权限
        /// </summary>
        /// <param name="rolecode"></param>
        /// <returns></returns>
        protected static string getrrlist(string rolecode, string rightscode, string extension)
        {
            Maticsoft.BLL.RoleRightRelation relabll = new Maticsoft.BLL.RoleRightRelation();
            return relabll.GetModelList("rolecode='" + rolecode + "'").Any(c => c.rightscode == rightscode && c.extension == extension) ? "checked=\"checked\"" : "";
        }
        protected string get(string rolecode, string rightscode)
        {
            Maticsoft.BLL.RoleRightRelation relabll = new Maticsoft.BLL.RoleRightRelation();
            return relabll.GetModelList("rolecode='" + rolecode + "'").Any(c => c.rightscode == rightscode) ? "checked=\"checked\"" : "";
        }
        protected string extension(string rightscode, string extension)
        {
            string str = "";
            if (!string.IsNullOrEmpty(extension))
            {
                foreach (var item in extension.Split('|'))
                {
                    str += " <label><input type=\"checkbox\" " + getrrlist(code.Value, rightscode, item) + " name=" + rightscode + " id=" + item + ">" + Enum.GetName(typeof(extension), Convert.ToInt32(item)) + "</label>";
                }
            }
            return str;
        }
    }
}